package com.sense.kukoo.common.repo.kv.data_version

import com.sense.kukoo.common.repo.kv.KukooKeyValueManager
import com.sense.kukoo.lib_logger.LoggerManager
import com.tencent.mmkv.MMKV

class DataVersionImpl(val mmkv: MMKV) {

    private val TAG: String = javaClass.simpleName

    fun updataVersionAndType(versionCode:Int, isRelease:Boolean){
        LoggerManager.d(TAG, "updataVersionAndType() versionCode=${versionCode} isRelease=${isRelease}")
        var isdeletaOldData = false
        val localVersion = mmkv.decodeInt(KukooKeyValueManager.KEY_DATA_VERSION_CODE, -1)
        val localIsRelease = mmkv.decodeBool(KukooKeyValueManager.KEY_DATA_VERSION_ISRELEASE, false)
        //check
        if(localVersion == -1){
            isdeletaOldData = true
        }
        if(isRelease != localIsRelease){
            isdeletaOldData = true
        }
        //clean
        if (isdeletaOldData) {
            LoggerManager.e(TAG, "删除旧数据!")
            mmkv.clearAll()
        }
        //save
        mmkv.encode(KukooKeyValueManager.KEY_DATA_VERSION_CODE, versionCode)
        mmkv.encode(KukooKeyValueManager.KEY_DATA_VERSION_ISRELEASE, isRelease)
    }
}